#include "UpdateProcess.h"

const string UpdateProcess::FINISH="hsinif";//Invert of "finish"
const string UpdateProcess::UNFINISH="hsinifnu";//Invert of "unfinish"

UpdateProcess::UpdateProcess(string cmdString,allTaskList *givenLists,allCommandStack *givenStacks):LogicProcess(cmdString,givenLists,givenStacks){
	needList=&allTask->getUpcomingTasks();
}

void UpdateProcess::HandleProcesss(){
	taskNumber=getTaskNumber();
	seekUpdateMode();
	Update();
	fromNeedtoDisplay();
}

void UpdateProcess::Update(){
	getTheLists();
	Task needTask;

	originList->retrieve(taskNumber,needTask);
	originList->remove(taskNumber);
	toUpdateList->add(needTask);

	pushToUndoList(needTask,Add,container1st,false);//For Undo
	pushToUndoList(needTask,Delete,container2nd,true);//For Undo
}

void UpdateProcess::getTheLists(){
	if (toFinish){
		originList=&allTask->getUpcomingTasks();
		toUpdateList=&allTask->getFinishedTasks();
		container1st=upcoming;
		container2nd=finished;
	}else{
		originList=&allTask->getFinishedTasks();
		toUpdateList=&allTask->getUpcomingTasks();
		container1st=finished;
		container2nd=upcoming;
	}
}
int UpdateProcess::getTaskNumber(){
	int result=0;
	string tskNum;
	tskNum=getInputTskNum();
	result=cvtToNumber(tskNum);
	return result;
}
string UpdateProcess::getInputTskNum(){
	string result;
	for (int i=0;i<commandString.length();i++){
		if (commandString[i]==' ')
			break;
		else
			result=result+commandString[i];
		
	}
	return result;
}
void UpdateProcess::seekUpdateMode(){
	string tmp=commandString;
	lowerCase(tmp);
	string mode;
	for (int i=tmp.length()-1;i>=0;i--){
		if (tmp[i]==' ')
			break;
		else
			mode=mode+tmp[i];
	}
	compareMode(mode);
}
bool UpdateProcess::compareMode(string mode){
	bool result=true;

	if (mode==FINISH)
		toFinish=true;
	else if (mode==UNFINISH)
		toFinish=false;
	else
		result=false;

	return result;
}
void UpdateProcess::lowerCase(string &tmp){
	for (int i=0;i<tmp.length();i++)
	{
		if (tmp[i] >= 'A' && tmp[i] <= 'Z')
			tmp[i] = tmp[i] + 32;
	}
}